// 车库

var Garage = DraggablePanelBase.extend({

    GARAGE_POS : cc.p(0, 642), // 基准坐标

    _garagePos : null,      // 车库真位置
    _side : 0,              // 车库方向
    _data : null,           // 坦克数据
    _tanks : {},          // UITank实例存放
    _tanksParent : null,    // UITanks的父层

    _deployEnabled : true,  // 是否可以部署

    ctor : function (garagePos) {
        this._super(0); // 优先级

        if(garagePos==undefined) { garagePos = this.GARAGE_POS; }
        this._garagePos = garagePos;

        //  UITanks实例的父Node
        this._tanksParent = cc.Node.create();
        this._tanksParent.setPosition(this._garagePos);
        this.addChild(this._tanksParent);
    },

    setDeployEnabled : function (isEnable) {
        this._deployEnabled = isEnable;
        if(!isEnable) {
            if (this.isDragging()) this.dragEnd(true);
        }
    },

    setTanksData : function (data, side) {
        this._side = side;
        this._data = data;
        this._tanks = {};

        // 显示车辆
        for each(var d in data) {
            var t = new UITank(d.id, d.type, side, d.pos[0], d.pos[1], d.hp, d.mhp);
            this._tanksParent.addChild(t);
            this._tanks[d.id] = t;
        }
    },

    getTanksData : function () {
        return this._data;
    },

    // 坦克移动之后调用, 用来同步数据
    onTankMove : function (uiTank) {
        var t = uiTank;
        for each(var d in this._data) {
            if(d.id == t.id) {
                d.pos[0] = t.cx;
                d.pos[1] = t.cy;
            }
        }

        //* 发送网络数据? 通知DataManager

    },

    getTankByCellPos : function (cx, cy) {
        //cc.log('getTankByCellPos:'+cx+'  '+cy);
        for(var id in this._tanks) {
            var t = this._tanks[id];
            var p = t.getCellPos();
            if (p.x == cx && p.y == cy) return t;
        }
    },

    onTouchBegan : function (touch, event) {
        //cc.log('GarageTouchPanel.onTouchBegan');
        if(!this._deployEnabled) return false;
        
        var tpos = touch.getLocation();

        if(this._isTouchIn(tpos)) {
            var cp = this._touchPos2Cell(tpos);

            // 如果点击的位置有坦克
            var t = this.getTankByCellPos(cp.x, cp.y);
            if (t) {
                this.dragBegin(t);  // 开始拖曳
                return true;
            }
        }
        return false;
    },

    onTouchMoved : function (touch, event) {
        this._super(touch, event);
    },

    onTouchEnded : function (touch, event) {
        //cc.log('GarageTouchPanel.onTouchEnded');
        if (!this.isDragging()) return;

        var tpos = touch.getLocation();
        //cc.dump(tpos);
        if(this._isTouchIn(tpos)) {
            var cp = this._touchPos2Cell(tpos);
            // 如果点击的位置有坦克
            var t = this.getTankByCellPos(cp.x, cp.y);
            if (t) {
                // 还原
                this.dragEnd(true);
            } else {
                var draggingTank = this.getDragTarget();
                this.dragEnd(); // 结束拖曳
                draggingTank.setCellPos(cp.x, cp.y, draggingTank.side);    // 放入新位置
                
                // 坦克移动
                this.onTankMove(draggingTank);
            }
        } 
        else {
            // 界外
            this.dragEnd(true);
        }
    },

    // 点击在范围内
    _isTouchIn : function (touchPos) {
        var p = cc.pSub(touchPos, this._garagePos);
        if (this._side == 1) {  // 上
            return (p.x >= 0 && p.x < helper.GARAGE_WIDTH && p.y >= 0 && p.y < helper.GARAGE_HEIGHT);    
        } else { // 下
            return (p.x >= 0 && p.x < helper.GARAGE_WIDTH && p.y <= 0 && p.y > -helper.GARAGE_HEIGHT);
        }
        
    },

    // touch坐标转成Cell坐标
    _touchPos2Cell : function (touchPos) {
        var p = cc.pSub(touchPos, this._garagePos);
        var cx = parseInt(p.x / helper.CELL_SIZE);
        var cy = -parseInt(p.y / helper.CELL_SIZE);
        if(this._side == 1) cy = -cy;

        return cc.p(cx, cy);
    }, 

});


